(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("documentService", documentService).
	filter("docMimeTypeFilter", [ '$filter', '$locale', 'documentService', '$translate', function($filter, $locale, documentService, $translate) {
		return function(optionValue, pattern) {
			return $filter('translate')(documentService.handDocMimeTypeLable(optionValue, pattern));
		};
	} ]);
	
	documentService.$inject = [ '$q', 'loggerFactory', 'modalService', 'gridService','ajaxServiceFactory' ,'storageService'];
	function documentService($q, loggerFactory, modalService, gridService, ajaxServiceFactory, storageService) {
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getService : {
				url : 'do/cda/dataList/service',
				method : 'get'
			},
			dropDownDataOptions : {
				url : 'do/cda/document/dropDownDataOptions',
				method : 'post'
			}
		});
		var documentServiceObj = this;
		function handDocMimeTypeLable(optionValue) {
			var options = documentServiceObj.mimeTypeOptions;
			var optionLabel = '';
			if(options) {
				angular.forEach(options.data, function(obj, index) {
					if(optionValue == obj.value) {
						optionLabel = obj.label;
					}
				});
			}
			return optionLabel;
		}
		function dropDownDataOptions(params) {
			var defer = $q.defer();
			ajaxService.dropDownDataOptions(params).then(function(response) {
				var options = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue.data = value;
						options[key] = tempValue;
					});
				}
				defer.resolve(options);
			});
			return defer.promise;
		}
		function initDropDownDataOptions(ctrl) {
			var defer = $q.defer();
			var params = {};
			if(ctrl && ctrl.document) {
				params.serviceCode = ctrl.document.serviceCode || 'CDA';
			} else {
				params.serviceCode = 'CDA';
			}
			dropDownDataOptions(params).then(function(response) {
				ctrl.docCatOptions = response.docCatOptions;
				ctrl.docCatOptions.hasEmptyOption = true;
				ctrl.docCatOptions.callback = {"changed" : "vm.docCatChanged"};
				ctrl.docSubCatOptions = {};
				ctrl.docSubCatOptions.hasEmptyOption = true;
				defer.resolve();
			}, angular.noop);
			ctrl.docCatChanged = function() {
				var docCat = ctrl.document.docCat;
				if(ctrl.docCatOptions && ctrl.docCatOptions.data)
				angular.forEach(ctrl.docCatOptions.data, function(docCatObj, index) {
					if(docCat == docCatObj.value) {
						ctrl.docSubCatOptions.data = docCatObj.extProperty.docSubCatOptions;
					}
				});
			}
			return defer.promise;
		}
		function initDocMimeTypeOptions(ctrl) {
			var defer = $q.defer();
			var params = {};
			if(ctrl && ctrl.document) {
				params.serviceCode = ctrl.document.serviceCode || 'CDA';
			} else {
				params.serviceCode = 'CDA';
			}
			dropDownDataOptions(params).then(function(response) {
				documentServiceObj.mimeTypeOptions = response.mimeTypeOptions;
				defer.resolve();
			}, angular.noop);
			return defer.promise;
		}
		function getServiceOptions() {
			var defer = $q.defer();
			ajaxService.getService().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getDoCumentStatus() {
			var options = {
				"data" : [ {
					"value" : "C",
					"label" : "Created"
				}, {
					"value" : "S",
					"label" : "delivered"
				} ]
			};
			return options;
		}
		
		return {
			getServiceOptions : getServiceOptions,
			getDoCumentStatus : getDoCumentStatus,
			initDropDownDataOptions : initDropDownDataOptions,
			initDocMimeTypeOptions : initDocMimeTypeOptions,
			handDocMimeTypeLable : handDocMimeTypeLable
		};
		
		function initDataOptions($scope, ctrl) {
			ctrl.localListOptions = getLocalsOptions();
			
			getServiceOptions().then(function(response) {
				ctrl.serviceCodeOptions = response.data;
			});
		}
		
	}
})(angular);